<?php session_start(); ?>
<html>
    <head>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" type="text/css" href="public/css/style.css" />
        <?php include('template/header.php')?>
<?php
require_once './model/works.php';
$md_works = new Works();
$id_work = $_GET['id'];


$params = array(
    'id_works' => $id_work
);
$work = $md_works->select($params);
?>
        <?php if (isset($_SESSION['id'])) { ?>
<form  method="post"      enctype="multipart/form-data">
    <div id="upload">
    <input type="text" name="name" value="<?php echo $work[0]['name']?>"><br>
    <input type="file" name="file"><br>
    <input type="submit" name="submit" value="Save">
    </div>
</form>

//<?php
//if (isset($_POST['submit'])) {
//    if (isset($_FILES['file']['name'])) {
//        move_uploaded_file($_FILES['file']['tmp_name'], 'public/images/'.$_FILES['file']['name']);
//    }
//    $value = array(
//       
//        'id'    =>  $id_work,
//        'image' => 'public/images/' .$_FILES['file']['name'],
//        'name'=>  $_POST['name'],
//    );
//    $md_works->update($value);
//    header('Location: index.php');
//}
//?>
<?php
if(isset($_POST["submit"])):
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 2000000 && $_FILES['file']['size']>100000)
) {
  if ($_FILES["file"]["error"] > 0) {
    echo "Error: " . $_FILES["file"]["error"] . "<br>";
  } else {
      $_FILES['file']['name']= time()."_".$_FILES["file"]["name"];
      if(move_uploaded_file($_FILES['file']['tmp_name'],"public/images/".$_FILES["file"]["name"])){
       $value=array(
           'id'=> isset($_GET['id'])?$_GET['id']:NULL,
           'name'=>  $_POST['name'],
//           'company_name'=>$_POST['company_name'],
//           'description'=>$_POST['des'],
           'image'=>"public/images/".$_FILES["file"]["name"]
       );       
       $update=$md_works->update ($value);
       if($update)
           header ('location:index.php');
       else {
           echo "Update fail";
       }
      }
      else echo 'Upload file khong thanh cong';
  }
} else {
  echo "Invalid file";
}
endif;
?>
<?php
                        }
                        else
                            header("location:index.php");
                        ?>

    <?php include('template/footer.php')?>
</html>
